﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineStore.Bll;
using OnlineStore.Dal;

namespace OnlineStore.Gui
{
    public partial class ChangeUser : System.Web.UI.Page
    {
        string PassWordNow,PassWordNew;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                tbCustomer user = (tbCustomer)Session["user"];
                if (Request.QueryString.Get("id") != "1")
                {
                    HL_tab_login.NavigateUrl = "/gui/Account.aspx";
                    HL_tab_login.Text = ((tbCustomer)Session["user"]).Username;
                    HL_tab_Logout.Visible = true;
                    tb_FullName.Text = user.Cus_Name;
                    tb_Address.Text = user.Address;
                    tb_Tel.Text = user.Phone;
                }
            }
            else
                Response.Redirect("/", true);
            if (IsPostBack)
            {
                if (tb_PassWordNew.Text == tb_PassWordAgain.Text)
                    PassWordNew = tb_PassWordNew.Text;
                else
                {
                    lb_WarningNew.Visible = true;
                    PassWordNew = "";
                }
                PassWordNow = tb_PassWordNow.Text;
            }
            if (Session["user"] != null)
            {
                HL_tab_login.NavigateUrl = "/gui/Account.aspx";
                HL_tab_login.Text = ((tbCustomer)Session["user"]).Username;
                HL_tab_Logout.Visible = true;
            }
            /*Giỏ hàng bên phải*/
            ListCartRight.DataSource = (List<bizCart>)Session["cart"];
            ListCartRight.DataBind();
            lb_Sum.Text = String.Format("{0:0,0 VNĐ}", bizCart.SumAllList((List<bizCart>)Session["cart"]));
            lb_Sum_product.Text = String.Format("{0:0,0 VNĐ}", bizCart.PriceOrder(bizCart.SumAllList((List<bizCart>)Session["cart"])));
            /**/
        }

        protected void Onsubmit(object sender, EventArgs e)
        {
            tbCustomer user = (tbCustomer)Session["user"];
            bizUser _bizUser = new bizUser();
            if (Request.QueryString.Get("id") != "1")
            {
                if (tb_Address.Text.Trim() == "" || tb_FullName.Text.Trim() == "" || tb_Tel.Text.Trim() == "")
                {
                    lb_warning.Visible = true;
                    return;
                }
                tbCustomer _user = _bizUser.UpdateInfo(user, tb_FullName.Text, tb_Address.Text, tb_Tel.Text);
                if (_user == null)
                {
                    lb_warning.Visible = true;
                    lb_warning.Text = "Thay đổi Thất bại";
                }
            }
            else
            {
                if (PassWordNew == "" || PassWordNow == "")
                {
                    lb_warning.Visible = true;
                }
                else
                {
                    tbCustomer _user = _bizUser.UpdatePassWord(user, PassWordNow, PassWordNew);
                    if (_user == null)
                        lb_warningNow.Visible = true;
                    else
                    {
                        Session["user"] = _user;
                        Response.Redirect("/Gui/Account.aspx", true);
                    }
                }
            }
        }
    }
}